package com.fs.zise;


import java.util.ArrayList;
import java.util.Vector;

import Tools.Animation;
import Tools.AnimationData;
import Tools.AnimationListener;
import Tools.BitmapTool;
import android.R.integer;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Bombs implements AnimationListener{
	
	private int bomb_row;
	private int bomb_col;
	public final static  int STATE_NONE = 0;
	public final static int STATE_FIRE = 1;
	public final static int STATE_EXPLOSION = 2;
	private int STATE_NOW = -1;
	private Bitmap bombBitmap = null;
	
	private Animation bomb_explosion_Animation;
 	private Bitmap bombExplosion = null;
	
	private  boolean bombExplosionOver;//爆炸结束
	private ArrayList<ExpoldeSpot> arrayListBombRange;//爆炸範圍

	private GameSurfaceView surfaceView;

	Bombs(GameSurfaceView surfaceView,int b_row,int b_col)
	{
		this.surfaceView = surfaceView;
		this.setBomb_row(b_row);
		this.setBomb_col(b_col);
		
		initBomb_Explosion();
		setBombExplosionOver(false);
		
	}
	
	
	private void initBomb_Explosion() {
		
		if(bombBitmap == null)
			bombBitmap = BitmapTool.getImageFromAssetFile("daoju.png");
		
		if(bombExplosion == null)
			bombExplosion = BitmapTool.getImageFromAssetFile("explode2.png");
		
		if(bomb_explosion_Animation == null)
				bomb_explosion_Animation = new Animation(new AnimationData("explode2.sprite",bombExplosion));
		bomb_explosion_Animation.setAction(0,1); //0  动作编号 1循环1次；
		bomb_explosion_Animation.setListener(this);
		arrayListBombRange = new ArrayList<ExpoldeSpot>();
	}


	public int getBOMB_STATE() {
		return STATE_NOW;
	}

	public Bombs setBOMB_STATE(int sTATE_NOW) {
		STATE_NOW = sTATE_NOW;
		
		return this;
	}

	public void bomb_draw(Canvas canvas,Paint paint)
	{
		if(!isBombExplosionOver())//没有爆炸的蛋
		{
			if(STATE_NOW == STATE_NONE)
				BitmapTool.drawClipImage(canvas, paint, bombBitmap, getBomb_col()*48+16, getBomb_row()*48+16, 192, 0, 16, 16);
			if(STATE_NOW == STATE_FIRE)
				BitmapTool.drawClipImage(canvas, paint, bombBitmap, getBomb_col()*48+16, getBomb_row()*48+16, 208, 0, 16, 16);
			if(STATE_NOW == STATE_EXPLOSION)
				showBombExplosion(canvas, paint);
		}
	}
	
	//爆炸范围9宫格
	int temp_bombCol;
	int temp_bombRow;
	
	private void getExplodeShunXuByI(int pos)
	{
		switch (pos) {
		case 0:
			temp_bombRow = getBomb_row() - 1;
			temp_bombCol = getBomb_col() - 1;
			break;
		case 1:
			temp_bombRow = getBomb_row() - 1;
			temp_bombCol = getBomb_col();
			break;
		case 2:
			temp_bombRow = getBomb_row() - 1;
			temp_bombCol = getBomb_col() + 1;
			break;
		case 3:
			temp_bombRow = getBomb_row() ;
			temp_bombCol = getBomb_col() - 1;
			break;
		case 4:
			temp_bombRow = getBomb_row() ;
			temp_bombCol = getBomb_col() ;
			break;
		case 5:
			temp_bombRow = getBomb_row() ;
			temp_bombCol = getBomb_col() +1;
			break;
		case 6:
			temp_bombRow = getBomb_row() + 1;
			temp_bombCol = getBomb_col() - 1;
			break;
		case 7:
			temp_bombRow = getBomb_row() + 1;
			temp_bombCol = getBomb_col() ;
			break;
		case 8:
			temp_bombRow = getBomb_row() + 1;
			temp_bombCol = getBomb_col() + 1;
			break;
		}
	}
	
	public void showBombExplosion(Canvas canvas,Paint paint)
	{
		for (int i = 0; i < 9; i++) {
				getExplodeShunXuByI(i);
			bomb_explosion_Animation.paint(canvas, paint, temp_bombCol*GameMap.getSingleInstance().getCellWidth()+16, temp_bombRow*GameMap.getSingleInstance().getCellHeight()+16);
		}
//		bomb_explosion_Animation.paint(canvas, paint, bomb_col*GameMap.getSingleInstance().getCellWidth()+24, bomb_row*GameMap.getSingleInstance().getCellHeight()+24);
		bomb_explosion_Animation.update(true);
	}

	@Override
	public void endofAnimation(Animation animation) {
		setBombExplosionOver(true);
		bomb_explosion_Animation.setVisible(false);
		setBOMB_STATE(STATE_NONE);
		GameMap.getSingleInstance().zise0_map0_one[getBomb_row()][getBomb_col()] = 30;
		System.out.println("监听爆炸动画，结束则自动修改地图块属性");
	}
	
	public boolean getBomb(int row,int  col)
	{
		if(this.getBomb_row() == row && this.getBomb_col() ==col )
			return true;
		return false;
	}


	public void setBomb_row(int bomb_row) {
		this.bomb_row = bomb_row;
	}


	public int getBomb_row() {
		return bomb_row;
	}


	public void setBomb_col(int bomb_col) {
		this.bomb_col = bomb_col;
	}


	public int getBomb_col() {
		return bomb_col;
	}


	public void setBombExplosionOver(boolean bombExplosionOver) {
		this.bombExplosionOver = bombExplosionOver;
	}


	public boolean isBombExplosionOver() {
		return bombExplosionOver;
	}
	
	public boolean explodeInLightPath() //没有爆炸的蛋才判断是否在光线上
	{
		for (int i = 0; i < surfaceView.mLaserEmitters.size(); i++) { 
			if(surfaceView.mLaserEmitters.get(i).getElementValueInLightPath(bomb_row,bomb_col) && !bombExplosionOver) 
				return true;
		}
		return false;
	}
	
	
	public  boolean searchExplodeRange(int orgR,int orgC) //被触摸的那个蛋的8格有没有其他蛋
	{
		arrayListBombRange.add(new ExpoldeSpot(bomb_row-1, bomb_col-1));
		arrayListBombRange.add(new ExpoldeSpot(bomb_row-1, bomb_col));
		arrayListBombRange.add(new ExpoldeSpot(bomb_row-1, bomb_col+1));
		
		//第二行
		arrayListBombRange.add(new ExpoldeSpot(bomb_row,bomb_col-1));
		arrayListBombRange.add(new ExpoldeSpot(bomb_row,bomb_col+1));
	
		//第三行
		arrayListBombRange.add(new ExpoldeSpot(bomb_row+1,bomb_col-1));
		arrayListBombRange.add(new ExpoldeSpot(bomb_row+1,bomb_col));
		arrayListBombRange.add(new ExpoldeSpot(bomb_row+1,bomb_col+1));
		
		for (int i = 0; i < arrayListBombRange.size(); i++) {
			if(orgR == arrayListBombRange.get(i).getExplodeR()&&orgC == arrayListBombRange.get(i).getExplodeC())
			{
				return true;
			}
		}
		return false;
	}
	
	class ExpoldeSpot
	{
		private int explodeR;
		private int explodeC;
		
		ExpoldeSpot(int explodeX,int explodeY)
		{
			this.setExplodeR(explodeX);
			this.setExplodeC(explodeY);
		}

		public void setExplodeR(int explodeR) {
			this.explodeR = explodeR;
		}

		public int getExplodeR() {
			return explodeR;
		}

		public void setExplodeC(int explodeC) {
			this.explodeC = explodeC;
		}

		public int getExplodeC() {
			return explodeC;
		}
		
	}
	

}
